package com.example.foodsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.dto.Result;
import com.example.foodsy.utils.Result;
import com.example.foodsy.entity.FarmInfo;
import com.example.foodsy.service.FarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 养殖企业产品批号信息表 前端控制器
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/farm-info")
@CrossOrigin
public class FarmInfoController {
	@Autowired
	private FarmInfoService farmInfoService;




	//查询所有产品批号
	@GetMapping("/getAllBatchId")
	public Result getAllBatchId(){
		List<String> batchIds = farmInfoService.getAllBatchId();
		return Result.ok(batchIds);
	}

	//查询所有待发布的产品批号
	@GetMapping("/getAllBatchIdByState/{farmId}")
	public Result getAllBatchIdByState(@PathVariable Integer farmId){
		List<FarmInfo> FarmInfoList = farmInfoService.list(new QueryWrapper<FarmInfo>().eq("farm_id", farmId).eq("state", 1));
		return Result.ok(FarmInfoList);
	}

	//查询所有已发布的产品批号
	@GetMapping("/getAllBatchIdByState2/{farmId}")
	public Result getAllBatchIdByState2(@PathVariable Integer farmId){
		List<FarmInfo> FarmInfoList = farmInfoService.list(new QueryWrapper<FarmInfo>().eq("farm_id", farmId).eq("state", 2));
		return Result.ok(FarmInfoList);
	}


	@GetMapping("/getByBatchId/{batchId}")
	public Result list(@PathVariable String batchId){
		FarmInfo  farmInfo = farmInfoService.getOne(new QueryWrapper<FarmInfo>().eq("batch_id", batchId));
		return Result.ok(farmInfo);
	}

	//根据企业编号查询
	@GetMapping("/getFarmInfoByFarmId/{farmId}")
	public Result getFarmInfoByFarmId(@PathVariable Integer farmId){
		List<FarmInfo> farmInfo = farmInfoService.list(new QueryWrapper<FarmInfo>().eq("farm_id", farmId));
		return Result.ok(farmInfo);
	}

	//根据产品批号进行查询
	@GetMapping("/getFarmInfoByBatchId/{batchId}")
	public Result getFarmInfoByBatchId(@PathVariable String batchId){
		FarmInfo farmInfo = farmInfoService.getOne(new QueryWrapper<FarmInfo>().eq("batch_id", batchId));
		return Result.ok(farmInfo);
	}

	//查询所有企业
	@GetMapping("/list")
	public Result list(){
		List<FarmInfo> farmInfo = farmInfoService.list();
		return Result.ok(farmInfo);
	}

	//通过fbId来查询batchId
	@GetMapping("/getBatchIdByFbId/{fbId}")
	public Result getBatchIdByFbId(@PathVariable String fbId){
		FarmInfo farmInfo = farmInfoService.getOne(new QueryWrapper<FarmInfo>().eq("fb_id", fbId));
		return Result.ok(farmInfo.getBatchId());
	}


	//getFarmInfoByFbId
	@GetMapping("/getInfoByFbId/{fbId}")
	public Result getFarmInfoByFbId(@PathVariable String fbId){
		AddressDTO<FarmInfo> addressDTO = farmInfoService.getInfoByFbId(fbId);
		return Result.ok(addressDTO);
	}


	//注册
	//注册内容包括：产品批号、产品品种、动物检疫合格证编号、检疫员名称
	@PostMapping("/saveFarmInfo")
	public Result save(@RequestBody FarmInfo farmInfo){

		if(farmInfoService.saveFarmInfo(farmInfo)){
			return Result.ok();
		}else {
			return Result.fail("保存失败");
		}
	}




	//根据批号下架处理
	@DeleteMapping("/removeByBatchId/{batchId}")
	public Result removeByBatchId(@PathVariable String batchId){
		FarmInfo farmInfo = farmInfoService.getByBatchId(batchId);
		farmInfo.setState(3);
		//逻辑删除
		return farmInfoService.updateById(farmInfo) ? Result.ok() : Result.fail("下架失败");
	}

	//更新
	@PostMapping("/editFarmInfo")
	public Result editFarmInfo(@RequestBody FarmInfo farmInfo){
		boolean b = farmInfoService.editFarmInfo(farmInfo);
		return b ? Result.ok() : Result.fail("修改失败11121321321");
	}




}
